﻿/**
*Page Utils class
*auth:lww
*version:1.0
*date:2014-10-11
*@param:context:Application
*/
function Router(context,config) {
    window.router = this;
    this.id = "";
    this.name = "Router";
    this.version = "1.0.0";
    this.mContext = context;
    this.mConfig = config;
    this.mMainPageObject = null;
    this.mDialogView = null;  
    this.showLoadDialog("...Loading...");  
    this.authInfo = null;
    this.menuList = null;
    this.currentMainNavMenuInfo = null;
    this.currentChildNavMenuInfo = null;  
    //set gloable key event
    document.onkeydown = this.keyDownEvent;
    document.onkeyup = this.keyUpEvent;
    document.oncontextmenu = new Function("return false;");
    //init page data
    this.init();
}
/**
*on Menu Event
*/
Router.prototype.onMenuEvent = function (menuItem) {   
    var ret = false;    
    //Main Nav Menu event,open and load left child menu page
    if (typeof (menuItem) != 'undefined' && menuItem != null) {
        this.currentMainNavMenuInfo = menuItem;
        if (this.mMainPageObject != null) {
            this.mMainPageObject.updateNavMenu();
            ret = true;               
        }
    }  
    //return 
    return ret;
};
Router.prototype.initMainMenuView = function (mainStyleName,itemStyleName) {   
    var router = this; 
    var menuList = router.menuList;
    var mTabMenuView = new TabMenuView(menuList);
    mTabMenuView.setClassName(mainStyleName);
    mTabMenuView.onSelected = function (index, viewItem, isSelected) {
        if (isSelected) { //menu selected event   
            router.onMenuEvent(viewItem.getData());                
        }
    }       
    if (menuList && menuList.getItemCount() > 0) {
        for (var i = 0; i < menuList.getItemCount(); i++) {
            var menuItem = menuList.getItem(i);                  
            mTabMenuView.addMenuItem(menuItem,itemStyleName);
        }       
    }
    return mTabMenuView;
};
/**
*showLoadDialog
*/
Router.prototype.showLoadDialog = function (tipInfo) {  
    this.mDialogView = new DialogLoadView(this.mContext);
    this.mDialogView.setRoundCorner(true);
    this.mDialogView.setWidth(140);
    this.mDialogView.setHeight(110);
    this.mDialogView.setLoadInfo(tipInfo);
    this.mDialogView.setBackground(new Drawable("#ffffff", null));
    this.mContext.openDialog(new LayoutParams(240, 160), true, this.mDialogView, true);
};
/**
*showTipDialog
*/
Router.prototype.showTipDialog = function (title, tipInfo,callbackFun) { 
    this.mDialogView = new DialogTipView(this.mContext);    
    if(callbackFun){
        this.mDialogView.onViewEvent = callbackFun;
    }       
    this.mDialogView.setTipInfo(title,tipInfo);
    this.mContext.openDialog(null, true, this.mDialogView, true);
};
/**
*open Dialog Page
*/
Router.prototype.openDialogPage = function (title, url, w, h, ico) {
    this.mDialogView = new DialogFrameView(this.mContext);
    this.mDialogView.setWidth(w);
    this.mDialogView.setHeight(h);
    this.mDialogView.setIsCanCloseByUser(true);
    this.mDialogView.setIco(ico);
    this.mDialogView.setFrameInfo(title, url, true);
    return this.mContext.openDialog(new LayoutParams(w, h), true, this.mDialogView, true);
};
/**
*close Dialog
*/
Router.prototype.closeDialog = function (arg) {
    this.mContext.closeDialog(arg);
};
/**
*load MainPage
*/
Router.prototype.loadMainPage = function () {
    if(!this.authInfo && !this.menuList){
        return; //check and return
    }
    this.mMainPageObject = new MainPage(this.mContext,this);
    this.mContext.setContentView(this.mMainPageObject);
    this.mMainPageObject.initPage();
};
/**
 * open exportData page
 */
Router.prototype.openExportPage = function (urlLocation, requestData, callbackFun, isTextResult) {      
    OpenWin(urlLocation,"导出数据",680,480,true,true);
};
/**
 * logout
 */
Router.prototype.logout = function () {
    window.location.href = "../logout"
};
Router.prototype.loadErrorAndExit = function(status, res, context){
    context.showLoadDialog(status+":"+res); 
    delay(3000).start(function(){goback();});
};
Router.prototype.parseLoadResult = function(res, context, isErrorExit){
    var resObj = JSON.parse(res);
    if(SUCCESS_CODE == resObj.code){
        return resObj;
    }  
    var exitFun = function(refObj, type){      
        if(isErrorExit){
            context.logout();
        }
    };
    context.showTipDialog("未授权", "code："+resObj.code+"<br/>msg："+resObj.message, exitFun);    
    return null;
};
/**
*init and check AuthInfo, 
*then open main page
*/
Router.prototype.init = function () {
    if(!getURLParams("_crsf")){ 
        goback();
    }  
    this.loadAuthInfo();   
};
Router.prototype.loadAuthInfo = function () {    
    httpGet(apiAuthInfo(this)).success(function(res,context){ 
        var resObj = context.parseLoadResult(res,context,true);
        if(isNull(resObj)){
            return;
        }
        context.authInfo = setCrsf(getAuthInfo(resObj));
        context.loadMenuInfo();
    }).then(this.loadErrorAndExit);
};
Router.prototype.loadMenuInfo = function () {
    httpGet(apiMenuInfo(this,this.authInfo)).success(function(res,context){ 
        context.menuList = getMenuInfo(context.parseLoadResult(res,context,true));
        updateMenuPath(context.menuList,"swagger",context.authInfo.getExtrasInfo("swagger"));
        context.loadMainPage();
    }).then(this.loadErrorAndExit);  
};
/**
 * aes function
 */
Router.prototype.aesEncrypt = function(plaintext,keys){
    var key = md5js.hexmd5(keys);   
    var keyBytes = aesjs.utils.utf8.toBytes(key);  
    var plainBytes = aesjs.utils.utf8.toBytes(plaintext);
    var paddingBytes = this.aesPaddingOrStrip(plainBytes,false);
    var aesEcb = new aesjs.ModeOfOperation.ecb(keyBytes);
    var encryptedBytes = aesEcb.encrypt(paddingBytes);     
    return aesjs.utils.hex.fromBytes(encryptedBytes);  
};
Router.prototype.aesDecrypt = function(cryptohex,keys){
    var key = md5js.hexmd5(keys);  
    var keyBytes = aesjs.utils.utf8.toBytes(key);  
    var aesEcb = new aesjs.ModeOfOperation.ecb(keyBytes);
    var cryptotoBytes = aesjs.utils.hex.toBytes(cryptohex);
    var paddingBytes = aesEcb.decrypt(cryptotoBytes);
    var plainBytes = this.aesPaddingOrStrip(paddingBytes,true);
    return aesjs.utils.utf8.fromBytes(plainBytes);      
};
Router.prototype.aesPaddingOrStrip = function(textBytes, isStrip){
    if(isStrip){
        return aesjs.padding.pkcs7.strip(textBytes); 
    }
    return aesjs.padding.pkcs7.pad(textBytes);  
};

markscript("Router");
//end Router.js